package com.mars.seckill.controller;

import com.mars.seckill.service.BuyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.annotation.Documented;

@RestController
public class BuyControllerImpl implements BuyController {

    @Autowired
    BuyService buyService;
    // 直接使用接口注入


    /**
     * 该方法先判断是否有资格参加秒杀，如果有资格的前提下再判断是否还有库存，即是否秒杀成功
     * @param item 本请求秒杀的商品
     * @param person 购买者
     * @return 返回购买者的购买情况 success || fail
     */

    @RequestMapping("/buy/{item}/{person}")
    @Override
    public String buyHandler(@PathVariable String item, @PathVariable String person) {
        boolean flag = true;
        if(flag&&buyService.canvisit(item,20,100)){
            int result = Integer.parseInt(buyService.buy(item,person));
            if(result>0)
                return person + " success";
            else
            {
                // 一旦某次购买因为货没了而失败，就改变标志位
                flag = false;
                return person + " fail";
            }
        }
        else{
            return person + " fail";
        }

    }
}
